package com.olda.play.system.biz.impl;

import com.olda.play.system.biz.SysDictionaryBizService;
import com.olda.play.system.model.dao.SysDictionaryMapper;
import com.olda.play.system.model.po.SysDictionary;
import com.olda.play.system.model.po.SysDictionaryExample;
import com.olda.play.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 */
@Service
public class SysDictionaryBizServiceImpl implements SysDictionaryBizService {

    @Autowired
    private SysDictionaryMapper sysDictionaryMapper;

    /**
     * 根据数据字典编码查询数据字典
     * @param dictCode
     * @return
     */
    @Override
    public SysDictionary selectSysDictBydictCode(String dictCode) {
        SysDictionaryExample example=new SysDictionaryExample();
        SysDictionaryExample.Criteria criteria=example.createCriteria();
        criteria.andDictCodeEqualTo(dictCode);

        List<SysDictionary> list=sysDictionaryMapper.selectByExample(example);

        return list.size()>0?list.get(0):null;
    }

    /**
     * 根据id查询数据字典
     * @param id
     * @return
     */
    @Override
    public SysDictionary selectSysDictById(Long id) {
        SysDictionary item=sysDictionaryMapper.selectByPrimaryKey(id);
        return item;
    }

    /**
     * 更新数据字典
     * @param sysDictionary
     */
    @Override
    public void updateSysDict(SysDictionary sysDictionary) {
        sysDictionary.setModifyTime(new Date());
        sysDictionary.setModifyUser(UserUtils.getUserId().toString());
        sysDictionary.setModifyUserName(UserUtils.getUsername());
        sysDictionaryMapper.updateByPrimaryKeySelective(sysDictionary);
    }

    /**
     * 根据字典编码删除数据字典
     * @param dictCode
     */
    @Override
    public void deleteSysDictBydictCode(String dictCode) {
        SysDictionaryExample example=new SysDictionaryExample();
        SysDictionaryExample.Criteria criteria=example.createCriteria();
        criteria.andDictCodeEqualTo(dictCode);

        SysDictionary sysDictionary =new SysDictionary();
        sysDictionary.setIsDeleted(1);
        sysDictionaryMapper.updateByExampleSelective(sysDictionary,example);
    }
}
